﻿using System.Web.Mvc;

namespace Rosieks.Web.Flow.Internals
{
    internal static class AjaxActionResultHelper
    {
        public static ActionResult OrAjaxEquivalent(this RedirectToRouteResult result, ControllerContext context)
        {
            if (context.HttpContext.Request.IsAjaxRequest())
            {
                var url = new UrlHelper(context.RequestContext);

                return new JavaScriptResult
                {
                    Script = string.Format("window.location = '{0}'", url.RouteUrl(result.RouteName, result.RouteValues)),
                };
            }

            return result;
        }

        public static ActionResult OrAjaxEquivalent(this RedirectResult result, ControllerContext context)
        {
            if (context.HttpContext.Request.IsAjaxRequest())
            {
                return new JavaScriptResult
                {
                    Script = string.Format("window.location = '{0}'", result.Url),
                };
            }

            return result;
        }
    }
}
